package main;

import java.util.Set;
import java.util.TreeSet;

public class IntroductoryProgrammingExercises {

    public static void main (String args[]) {

        IntroductoryProgrammingExercises introductoryProgrammingExercises = new IntroductoryProgrammingExercises();

        System.out.println("Print one asterisk to hte console");
        introductoryProgrammingExercises.drawAsterisk();

        System.out.println();

        System.out.println("print n asterisks on one line");
        introductoryProgrammingExercises.drawHorizontalLineWithAsterisk(8);

        System.out.println();

        System.out.println("Given a number n, print n lines, each with one asterisks");
        introductoryProgrammingExercises.drawVerticalLineWithAsterisk(8);

        System.out.println();

        System.out.println("Given a number n, print n lines, each with one more asterisk than the last");
        introductoryProgrammingExercises.drawRightTriangle(3);

        System.out.println();

        System.out.println("Given a number n, print a centered triangle");
        introductoryProgrammingExercises.drawIsoscelesTriangle(3);

        System.out.println();

        System.out.println("Given a number n, print a centered diamond");
        introductoryProgrammingExercises.drawDiamond(3);

        System.out.println();

        System.out.println("Given a number n, print a centered diamond with your name in place of the middle line");
        introductoryProgrammingExercises.drawDiamondWithName("Cherish", 3);

        System.out.println();

        System.out.println("FizzBuzz Game");
        introductoryProgrammingExercises.fizzBuzz();

        System.out.println();

        System.out.println("Calculate Prime Factors");
        introductoryProgrammingExercises.generate(30);

    }

    public void drawAsterisk() {
        System.out.println("*");
    }

    public void drawHorizontalLineWithAsterisk(int number) {
        for (int count = 1; count <= number; count++) {
            System.out.print("*");
        }
    }

    public void drawVerticalLineWithAsterisk(int number) {
        for (int count = 1; count <= number; count++) {
            System.out.println("*");
        }
    }

    public void drawRightTriangle(int number) {
         for (int count = 1; count <= number; count++) {
             drawSignAccordingNumber("*", count);
             drawSignAccordingNumber(" ", number - count);
             System.out.println();
         }
    }

    private void drawSignAccordingNumber(String sign, int number) {
        for (int count = 1; count <= number; count++) {
            System.out.print(sign);
        }
    }

    public void drawIsoscelesTriangle(int sideLength) {
        for (int count = 1; count <= sideLength; count++) {
            drawSignAccordingNumber(" ", sideLength - count);
            drawSignAccordingNumber("*", 2 * count - 1);
            drawSignAccordingNumber(" ", sideLength - count);
            System.out.println();
        }
    }

    public void drawDiamond(int sideLength) {
        drawIsoscelesTriangle(sideLength);
        drawDelOperator(sideLength);
    }

    private void drawDelOperator(int sideLength) {
        for (int count = 1; count <= sideLength - 1; count++) {
            drawSignAccordingNumber(" ", count);
            drawSignAccordingNumber("*", 2 * (sideLength - count) - 1);
            drawSignAccordingNumber(" ", count);
            System.out.println();
        }
    }

    public void drawDiamondWithName(String name, int sideLength) {
        for (int count = 1; count <= sideLength - 1; count++) {
            drawSignAccordingNumber(" ", sideLength - count);
            drawSignAccordingNumber("*", 2 * count - 1);
            drawSignAccordingNumber(" ", sideLength - count);
            System.out.println();
        }
        System.out.println(name);
        drawDelOperator(sideLength);
    }

    public void fizzBuzz() {
        for (int count =1; count <= 100; count++) {

            if (count % 3 != 0 && count % 5 != 0 ) {
                System.out.println(count);
                continue;
            }

            if (count % 3 == 0) {
                System.out.print("Fizz");
            }

            if (count % 5 == 0) {
                System.out.print("Buzz");
            }

            System.out.println();
        }
    }

    public void generate(int number) {
        Set<Integer> primeFactors = new TreeSet<Integer>();
        int temp = number;
        for (int count = 2; count < number; count++) {
             while (temp % count == 0) {
                 primeFactors.add(count);
                 temp = temp / count;
             }
        }

        for(Integer primeFactor : primeFactors)  {
            System.out.print(primeFactor + " ");
        }
    }
}
